package com.atguigu.lease.web.admin.service.imp;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    CityInfoMapper cityInfoMapper;

    @Autowired
    DistrictInfoMapper districtInfoMapper;

    // 配套中间表
    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;

    // 杂费中间表
    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;

    //公寓标签中间表
    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;

    //图片表
    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    FeeValueMapper feeValueMapper;

    @Transactional
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {

        //设置区域信息
        Long provinceId = apartmentSubmitVo.getProvinceId();
        String provinceName = provinceInfoMapper.selectById(provinceId).getName();

        Long cityId = apartmentSubmitVo.getCityId();
        String cityName = cityInfoMapper.selectById(cityId).getName();

        Long districtId = apartmentSubmitVo.getDistrictId();
        String districtName = districtInfoMapper.selectById(districtId).getName();

        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);
        super.saveOrUpdate(apartmentSubmitVo); // 这里保存后才会生成id 所以后面要重新拿到这个id

        Long apartmentId = apartmentSubmitVo.getId();

        if (apartmentId != null){
            //更新
            //删除公寓配套
            LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper =new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityMapper.delete(lambdaQueryWrapper);

            //删除公寓标签
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper =new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentId);
            apartmentLabelMapper.delete(labelLambdaQueryWrapper);

            //删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper =new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentId);
            apartmentFeeValueMapper.delete(feeValueLambdaQueryWrapper);

            //删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentId);
            graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

        }

        //插入公寓配套
        apartmentFacilityMapper.insertBatch(apartmentId,apartmentSubmitVo.getFacilityInfoIds());
        //插入公寓标签
        apartmentLabelMapper.insertBatch(apartmentId,apartmentSubmitVo.getLabelIds());
        //插入杂费
        apartmentFeeValueMapper.insertBatch(apartmentId,apartmentSubmitVo.getFeeValueIds());

        //插入图片
        graphInfoMapper.insertBatch(1,apartmentId,apartmentSubmitVo.getGraphVoList());
    }

    @Override
    public IPage<ApartmentItemVo> selectPageList(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.selectPageList(iPage,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailByApartmentId(Long id) {
        //赋值基础信息
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = baseMapper.selectById(id);

        //赋值图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, 1);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);

        // 如果有需要排序，过滤，分组可以优先使用这种，因为写for循环会很麻烦
        List<GraphVo> graphVos =graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            return graphVo;
        }).collect(Collectors.toList());
        // 更简单常见
//        List<GraphVo> graphVos = new ArrayList<>();
//        for (GraphInfo graphInfo : graphInfos) {
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo, graphVo);
//            graphVos.add(graphVo);
//        }


        //赋值公寓配套
        List<LabelInfo> labelInfoList =labelInfoMapper.selectByApartmentId(id);

        //赋值标签
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByApartmentId(id);

        //赋值杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectByApartmentId(id);

        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVos);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Transactional
    @Override
    public void removeApartmentById(Long id) {

        //删除公寓配套
        LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityMapper.delete(lambdaQueryWrapper);

        //删除公寓标签
        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper =new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelMapper.delete(labelLambdaQueryWrapper);

        //删除杂费
        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper =new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueMapper.delete(feeValueLambdaQueryWrapper);

        //删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

        // 删除apartment_info
        baseMapper.deleteById(id);


    }
}




